// @charset "utf-8";

define(function(require, exports, module) {
	var publicMethod = require('../public/public'),
		clientApi = require('../public/clientapi'),
		clientStorage = require('../public/storage');

	var renderDefault = {
		addHistoryFile: 'addhistory.log',
		getAddHistory: async function() {
			var self = this;
			var addHistory = await clientStorage.getFileContent(self.addHistoryFile);
			if (addHistory == false) {
				self.addHistory = [];
			} else {
				self.addHistory = addHistory;
			}
			self.renderHistory();
		},
		addHistory: [{
			type: 'zqxy',
			title: '赚钱效应',
			addtime: '2017-03-02',
			state: 1

		},{
			type: 'table',
			title: '股价小于10',
			thtml: '',
			addtime: '2017-03-03',
			state: 0

		}],

		renderHistory: function() {
			var self = this;
			if (self.addHistory.length == 0) {
				$('#add-history-ul').html('<li><p class="no-add-record">你还未添加过！</p></li>')
			} else {
				var ulHtml = [];
				var addHistory = self.addHistory.slice(0, 20);
				$.each(addHistory, function(i, val) {
					var liHtml = '';
					var addTime = val.addtime;
					var btnHtml = '';
					if (val.state == 1) {
						btnHtml = '<a href="###" class="icon s-m-add '+ val.type +'HistAddBtn" title="添加到右侧"></a>' +
									'<a href="###" class="icon s-m-del hide '+ val.type +'HistDelBtn" title="从右侧删除"></a>';
					} else {
						btnHtml = '<a href="###" class="icon s-m-add hide '+ val.type +'HistAddBtn" title="添加到右侧"></a>' +
									'<a href="###" class="icon s-m-del '+ val.type +'HistDelBtn" title="从右侧删除"></a>';
					}
					if (val.type == 'table') {

						liHtml = '<li class="history-li" data-s-title="'+ val.type +'" data-cond="'+ val.title +'">' +
							'<span class="add-time">'+ addTime +'</span>' +
							'<div class="system-module-item">' +
								'<div class="s-m-top">' +
									btnHtml +
									'<p>'+ val.title +'</p>' +
								'</div>' + val.thtml + '</div>' +
						'</li>';
					} else {
						liHtml = '<li class="history-li" data-s-title="'+ val.type +'">' +
							'<span class="add-time">'+ addTime +'</span>' +
							'<div class="system-module-item">' +
								'<div class="s-m-top">' +
									btnHtml +
									'<p>'+ val.title +'</p>' +
								'</div>' +
								'<img src="http://i.thsi.cn/images/client/exammarket/'+ val.type +'demo.jpg" alt="">' +
							'</div>' +
						'</li>';
					}
					ulHtml.push(liHtml);
				});
				$('#add-history-ul').html(ulHtml.join(''));
			}

		},
		saveAddHistory: async function (item) {
			var self  = this;
			var type = item.type;
			var title = item.title;
			$.each(self.addHistory, function (i, val) {
				if (val.type == 'table' && val.title == title) {
					val.state = val.state == 1 ? 0 : 1;
				} else if (val.type == type) {
					val.state = val.state == 1 ? 0 : 1;
				}
			});
			self.addHistory.unshift(item);
			self.addHistory.slice(0, 20);

			await clientStorage.writeFileContent(self.addHistoryFile, JSON.stringify(self.addHistory));

		},
		changeState: async function (type, title) {
			var self = this;
			$.each(self.addHistory, function (i, val) {
				if (val.type == 'table' && val.title == title) {
					val.state = val.state == 1 ? 0 : 1;
				} else if (val.type == type) {
					val.state = val.state == 1 ? 0 : 1;
				}
			});

			await clientStorage.writeFileContent(self.addHistoryFile, JSON.stringify(self.addHistory));
		}
	};

	module.exports = renderDefault;
});